<template>
	<view>
		<view class="uni-padding-wrap uni-common-mt">
			<text>请核查商品</text>
		</view>
		<uni-card
				v-for="item in goodsCheck"
				:key="item.id"
		    :title="item.info"
		    mode="style"
		    :is-shadow="true"
		    :thumbnail="item.image"
		    :note="item.piceCs"
		    :status="item.count"
				:stock="item.stock"
				:size="item.size"
		>
		商品名称: {{ item.name }}
		</uni-card>
		<view class="uni-padding-wrap uni-common-mt">
			<button type="primary" @click="purchase">立即购买</button>
		</view>
	</view>
</template>

<script>
	import uniCard from "@/components/uni-card/uni-card"
	export default {
		components: { uniCard },
		data() {
			return {
				goodsCheck: '',
				sellerId: ''
			}
		},
		computed: {
			// 总价
			getAllPrice() {
				let result = 0;
				let result1 = 0;
				this.goodsCheck.forEach((food) => {
					result1 += this.accMul(food.count, food.piceCs)
					result = result1.toFixed(2);
				})
				return result
			}
		},
		onLoad(option) {
			this.sellerId = option.id
			this.goodsCheck = uni.getStorageSync('checkGoods')
		},
		methods: {
			// 立即购买
			purchase() {
				let order = {
					//订单类型（0：经销商-厂家订单）（1：农户-经销商订单）
					orderType: 0,
					// 支付类型（0,：线下支付）（1：线上支付）
					payType: 1,
					// (线下支付)支付金额
					orderMent: this.getAllPrice,
					// 售卖人角色ID
					sellerId: uni.getStorageSync('user').superiorid,
					// 购买人角色id
					userId: this.$store.getters.getUser.roleid,
				}
				let productList = []
				this.goodsCheck.forEach((item) => {
					let arrProduct = {
						id: item.id,
						name: item.name,
						piceCs: item.piceCs,
						stock: item.count
					}
					productList.push(arrProduct)
				})
				let params = {
					order,
					productList
				}
				uni.request({
					url: '/xnt/order/add',
					method: 'POST',
					data: {
						order,productList
					},
					success: (res) => {
						console.log(res.data)
						let data = res.data
						if (data.success) {
							let params = {
								orderno: data.rows
							}
							this.$api.orderPayOrder(params).then((res) => {
								if(res.success) {
									window.location.href=res.rows 
								}else {
									uni.showToast({
										icon: 'none',
										title: '失败',
										duration: 2000
									});
								}
							}).catch((err) => {
								console.log('request fail', err);
							})
						}
					}
				})
			},
			// 解决浮点数 运算出现多位小数
			accMul(arg1, arg2) {
				let m = 0,
					s1 = '',
					s2 = '';
				if (arg1 && arg1 != null)
					s1 = arg1.toString();
				if (arg2 && arg2 != null)
					s2 = arg2.toString();
				// console.log('m1',s2.replace('.',''))
				try {
					m += s1.split('.')[1].length
				} catch (e) {}
				try {
					m += s2.split('.')[1].length
				} catch (e) {}
			
				return Number(s1.replace('.', '')) * Number(s2.replace('.', '')) / Math.pow(10, m);
			},
		}
	}
</script>

<style>
</style>
